package com.bw.sjld.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.sjld.domain.Sjld;
import com.bw.sjld.service.SjldService;
import com.bw.sjld.units.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sjld")
public class SjldController {
    @Autowired
    private SjldService sjldService;
    @RequestMapping("/getAll")
    public Result getAll(){
        List<Sjld> list = sjldService.list();
        return Result.success(list);
    }
    @RequestMapping("/getsids")
    public Result list(){
        Integer pid=0;
        LambdaQueryWrapper<Sjld> sjldLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sjldLambdaQueryWrapper.eq(Sjld::getPid,pid);
        List<Sjld> list = sjldService.list(sjldLambdaQueryWrapper);
        return Result.success(list);
    }
    @RequestMapping("/getcids")
    public Result getcids(Integer pid){
        LambdaQueryWrapper<Sjld> sjldLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sjldLambdaQueryWrapper.eq(Sjld::getPid,pid);
        List<Sjld> list = sjldService.list(sjldLambdaQueryWrapper);
        return Result.success(list);
    }
    @RequestMapping("/nihao")
    public List<Tree<String>> nihao(){
        List<Sjld> nodeList = sjldService.list();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都有默认值的
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("rid");
        treeNodeConfig.setNameKey("name");
// 最大递归深度
        treeNodeConfig.setDeep(3);

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<Sjld, String>build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getPid()+"");
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
//                    tree.putExtra("extraField", 666);
//                    tree.putExtra("other", new Object());
                });
        return treeNodes;
    }
}
